<?php

namespace app\index\controller;

class Home extends Base
{
    public function index()
    {
        return $this->fetch('index');
    }

    //大图移动
    public function moveMap()
    {
        $where[] = ['qylv', '<=', $this->roleInfo['ulv']];
        $list = model('Map')->getList($where, 'qylv asc,qyid desc');
        $this->assign('list', $list);

        return $this->fetch('map');
    }

    //区域移动
    public function moveArea()
    {
        try {
            $request = request();
            if ($request->method() == 'POST') {
                $modelArea = model('MapArea');

                $nowmid = input('mid', 225);
                if ($nowmid == 0 || $nowmid == '') {
                    $nowmid = 225;
                }

                //获取移动前的位置
                $mname = $modelArea->getWhereValue(['mid' => $nowmid], 'mname');

                //更新移动位置
                model('Role')->where('uid', $this->role_id)->update(['nowmid' => $nowmid]);

                //添加移动信息
                $playerinfo = $this->roleInfo['uname'] . " 向 " . $mname . " 走去";
                $modelArea->where('mid', $nowmid)->update(['playerinfo' => $playerinfo]);

                $res['url'] = url('index');
                returnJson(1, 'ok', $res);
            } else {
                returnJson(0, '非法请求');
            }
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    //获取聊天信息
    public function getMessage()
    {
        $page = input('page', 1);
        $num = 5;
        $limit = ($page - 1) * $num;

        $list = model('Message')->order('id desc')->limit($limit, $num)->select();

        $total = model('Message')->getCountNum();
        $data = [
            'list' => $list,
            'pages' => ceil($total / $num)
        ];
        returnJson(1, 'ok', $data);
    }

    //npc详情
    public function npc()
    {
        try {
            $temp = 'npc';

            $id = input('id');
            $info = model('Npc')->getInfo($id);
            if (!empty($info)) {
                $info['task_list'] = [];
                if ($info['taskid'] != '') {
                    $ary = [];
                    $mnpcAry = explode(',', $info['taskid']);
                    foreach ($mnpcAry as $k => $item) {
                        $infoTask = model('Task')->getInfo($item);

                        $rwlx = $infoTask['rwlx'] == 1 ? "普通" : ($infoTask['rwlx'] == 2 ? '每日' : '主线');
                        $rwzl = $infoTask['rwzl'] == 1 ? "收集" : ($infoTask['rwzl'] == 2 ? '打怪' : '对话');

                        $ary[$k]['id'] = $item;
                        $ary[$k]['status'] = 1; //任务状态 status:1未接取，2已接取
                        $ary[$k]['name'] = '[' . $rwlx . '][' . $rwzl . '][lv:' . $infoTask['rwlv'] . ']-' . $infoTask['rwname'];

                        //普通和主线任务
                        $map = [];
                        $map[] = ['uid', '=', $this->role_id];
                        $map[] = ['rwid', '=', $item];
                        $map[] = ['rwlx', 'in', '1,3'];
                        $infoMy = model('RoleTask')->getWhereInfo($map);
                        //验证是否已接取
                        if (!empty($infoMy)) {
                            //验证是否完成 continue
                            if ($infoMy['rwzt'] == 3) {
                                unset($ary[$k]);
                                continue;
                            }
                            $ary[$k]['status'] = 2;
                        }
                        //每日任务
                        $map2 = [];
                        $map2[] = ['uid', '=', $this->role_id];
                        $map2[] = ['rwid', '=', $item];
                        $map2[] = ['rwlx', '=', 2];
                        $map2[] = ['data', '=', date('d')];
                        $infoMy2 = model('RoleTask')->getWhereInfo($map2);
                        if (!empty($infoMy2)) {
                            if ($infoMy2['rwzt'] == 3) {
                                unset($ary[$k]);
                                continue;
                            }
                            $ary[$k]['status'] = 2;
                        }

                        //如果是对话任务
                        if ($infoTask['rwzl'] == 3) {
                            //验证是否已接
                            $map3 = [];
                            $map3[] = ['uid', '=', $this->role_id];
                            $map3[] = ['rwid', '=', $item];
                            $map3[] = ['rwzl', '=', 3];
                            $infoMy3 = model('RoleTask')->getWhereInfo($map3);
                            if (!empty($infoMy3)) {//已接
                                if ($infoTask['rwcount'] != $id) {
                                    unset($ary[$k]);
                                    continue;
                                }
                            } else {//未接
                                if ($infoTask['rwyq'] != $id) {
                                    unset($ary[$k]);
                                    continue;
                                }
                            }
                        }

                    }
                    $info['task_list'] = $ary;
                }

                if ($info['muban'] != '') {
                    if($info['muban'] == 'sd.php'){
                        $info['list'] = model('Drug')->getList([],'yplv asc,ypid asc');
                        $temp = '../temp/shop';
                    }
                    if($info['muban'] == 'zl.php'){
                        $temp = '../temp/treat';
                    }
                    if($info['muban'] == 'club.php'){
                        $temp = '../temp/club';
                    }
                }
            }
            $this->assign('info', $info);
            //echo "<pre>";print_r($info);die;

            return $this->fetch($temp);
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    //任务详情
    public function task()
    {
        try {
            //任务状态 status:1未接取，2可提交，3未完成
            $status = 1;
            $task_id_my = 0;

            $id = input('id');
            $nid = input('nid');

            $info = model('Task')->getInfo($id);

            $map = [];
            $map[] = ['uid', '=', $this->role_id];
            $map[] = ['rwid', '=', $id];
            $infoMy = model('RoleTask')->getWhereInfo($map);
            if (!empty($infoMy)) {
                //已接
                $status = 3;
                $task_id_my = $infoMy['id'];

                //验证任务已接取状态下是：可提交，未完成
                if ($infoMy['rwzl'] == 3) {
                    $status = 2;
                } else {
                    if ($infoMy['rwcount'] == $infoMy['rwnowcount']) {
                        $status = 2;
                    }
                }
            }
            if (!empty($info)) {
                $info['rwlxName'] = $info['rwlx'] == 1 ? "普通" : ($info['rwlx'] == 2 ? '每日' : '主线');
                $info['rwzlName'] = $info['rwzl'] == 1 ? "收集" : ($info['rwzl'] == 2 ? '打怪' : '对话');

                if ($info['rwzl'] == 1) {
                    $info['rwyq_name'] = model('Prop')->findValue(['djid' => $info['rwyq']], 'djname');
                }
                if ($info['rwzl'] == 2) {
                    $info['rwyq_name'] = model('Monster')->findValue(['id' => $info['rwyq']], 'gname');
                }
                if ($info['rwzl'] == 3) {
                    $info['rwyq_name'] = model('Npc')->findValue(['id' => $info['rwyq']], 'nname');
                    $info['rwcount_name'] = model('Npc')->findValue(['id' => $info['rwcount']], 'nname');
                }

                $info['status'] = $status;
                $info['task_id_my'] = $task_id_my;
                $info['npc_id'] =  $nid;
            }
            $this->assign('info', $info);

            return $this->fetch('task');
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    //接取或提交任务
    public function setTask()
    {
        try {
            $request = request();
            if ($request->method() == 'POST') {
                $modelTask = model('Task');
                $modelRoleTask = model('RoleTask');
                $roleInfo = $this->roleInfo;

                $status = input('status');  //任务状态 status:1未接取，2可提交，3未完成
                $rwid = input('rwid');
                $task_id_my = input('task_id_my');
                $npc_id = input('npc_id');

                $infoNpc = model('Npc')->getInfo($npc_id);
                if (empty($infoNpc) || $npc_id==0) {
                    returnJson(0, '任务Npc异常');
                }
                $npcTastAry = explode(',', $infoNpc['taskid']);
                if(!in_array($rwid,$npcTastAry)){
                    returnJson(0, $infoNpc['nname'] . '：我没有此任务哦');
                }

                $infoTask = $modelTask->getInfo($rwid);
                if(empty($infoTask)){
                    returnJson(0, '任务不存在');
                }

                if ($status == 1) {
                    if($infoTask['rwlv'] > $roleInfo['ulv']){
                        returnJson(0, '请先提升你的等级！');
                    }

                    //验证前置是否完成
                    if ($infoTask['lastrwid'] > 0) {
                        $map['uid'] = $this->role_id;
                        $map['rwid'] = $infoTask['lastrwid'];
                        $map['rwzt'] = 3;
                        $infoRoleTaskLast = $modelRoleTask->getWhereInfo($map);
                        if (empty($infoRoleTaskLast)) {
                            $infoLastTask = $modelTask->getInfo($infoTask['lastrwid']);
                            returnJson(0, '请先完成前置任务<' . $infoLastTask['rwname'] . '>');
                        }
                    }

                    //验证是否已接
                    $mapTask['uid'] = $this->role_id;
                    $mapTask['rwid'] = $rwid;
                    $infoRoleTask = $modelRoleTask->getWhereInfo($mapTask);
                    if (!empty($infoRoleTask)) {
                        if($infoRoleTask['rwlx']==2 && $infoRoleTask['data']==date('d')){
                            returnJson(0, '不可重复接取任务');
                        }
                        if($infoRoleTask['rwlx']==1 || $infoRoleTask['rwlx']==3){
                            returnJson(0, '不可重复接取任务');
                        }
                    }

                    $add['uid'] = $this->role_id;
                    $add['rwid'] = $rwid;
                    $add['rwname'] = $infoTask['rwname'];
                    $add['rwinfo'] = $infoTask['rwinfo'];
                    $add['rwlv'] = $infoTask['rwlv'];
                    $add['rwlx'] = $infoTask['rwlx'];
                    $add['rwzl'] = $infoTask['rwzl'];
                    $add['rwyq'] = $infoTask['rwyq'];
                    $add['rwcount'] = $infoTask['rwcount'];
                    $add['rwdj'] = $infoTask['rwdj'];
                    $add['rwyp'] = $infoTask['rwyp'];
                    $add['rwzb'] = $infoTask['rwzb'];
                    $add['rwjineng'] = $infoTask['rwjineng'];
                    $add['rwexp'] = $infoTask['rwexp'];
                    $add['rwyxb'] = $infoTask['rwyxb'];

                    if ($infoTask['rwlx'] == 2) {
                        $add['data'] = date('d');
                    }
                    if ($infoTask['rwzl'] == 3) {
                        $add['issuer_npc_id'] = $infoTask['rwyq'];
                        $add['handover_npc_id'] = $infoTask['rwcount'];
                    }else{
                        $add['issuer_npc_id'] = $npc_id;
                        $add['handover_npc_id'] = $npc_id;
                    }

                    $code = $modelRoleTask->add($add);
                } else {
                    $map['id'] = $task_id_my;
                    $map['uid'] = $this->role_id;
                    $map['rwid'] = $rwid;
                    $infoRoleTask = $modelRoleTask->getWhereInfo($map);
                    if (empty($infoRoleTask)) {
                        returnJson(0, '任务不存在');
                    }
                    if($infoRoleTask['rwzt']==3){
                        returnJson(0, '任务已完成');
                    }
                    if ($infoRoleTask['rwzl'] != 3 && $infoRoleTask['rwcount'] != $infoRoleTask['rwnowcount']) {
                        returnJson(0, '任务尚未达到完成标准。');
                    }

                    $temp = [
                        'id' => $task_id_my,
                        'rwzt' => 3
                    ];
                    $code = $modelRoleTask->saved($temp);
                    if ($code) {
                        $this->strSelect($infoRoleTask['rwzb'], 1);
                        $this->strSelect($infoRoleTask['rwdj'], 2);
                        $this->strSelect($infoRoleTask['rwyp'], 3);

                        $updata = [
                            'uid' => $this->role_id,
                            'uexp' => $roleInfo['uexp'] + $infoRoleTask['rwexp'],
                            'uyxb' => $roleInfo['uyxb'] + $infoRoleTask['rwyxb'],
                        ];
                        model('Role')->saved($updata);
                    }
                }

                if ($code !== false) {
                    returnJson(1, 'ok', ['url' => url('index')]);
                } else {
                    returnJson(0, '操作失败');
                }
            } else {
                returnJson(0, '非法请求');
            }
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    public function strSelect($strIds, $type)
    {
        $ary = ($strIds != '' && $strIds != null) ? explode(',', $strIds) : [];
        if (!empty($ary)) {
            foreach ($ary as $k => $item) {
                $aryInfo = explode('|', $item);
                if ($type == 1) {
                    model('RoleEquip')->addzb($this->role_id, $aryInfo[0]);
                }
                if ($type == 2) {
                    model('RoleProp')->adddj($this->role_id, $aryInfo[0], $aryInfo[1]);
                }
                if ($type == 3) {
                    model('RoleDrug')->addyp($this->role_id, $aryInfo[0], $aryInfo[1]);
                }
            }
        }
    }

    //治疗
    public function treat()
    {
        try {
            $request = request();
            if ($request->method() == 'POST') {
                $roleInfo = $this->roleInfo;
                $ulv = $roleInfo['ulv'];
                if ($ulv < 10) {
                    $consume = 0;
                } else {
                    $consume = round($ulv * $ulv / 10 + $ulv / 2);
                }

                if ($roleInfo['uyxb'] < $consume) {
                    returnJson(0, '少侠，你的诊金不足！');
                }
                $uyxb = $roleInfo['uyxb'] - $consume;

                $temp = [
                    'uid' => $roleInfo['uid'],
                    'uyxb' => round($uyxb),
                    'uhp' => $roleInfo['umaxhp']
                ];
                model('Role')->saved($temp);
                returnJson(1, '少侠，你已恢复健康！[花费：'.$consume.']');
            } else {
                returnJson(0, '非法请求');
            }
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    //采购
    public function buyDrug()
    {
        try {
            $request = request();
            if ($request->method() == 'POST') {
                $shop_id = input('shop_id');
                $cart_num = input('cart');

                $roleInfo = $this->roleInfo;

                $infoDrug = model('Drug')->getInfo($shop_id);
                if(empty($infoDrug)){
                    returnJson(0, '此商品已下架');
                }

                $money = $cart_num * $infoDrug['ypjg'];
                if($money > $roleInfo['uyxb']){
                    returnJson(0, '灵石不足');
                }

                $getId = model('RoleDrug')->addyp($roleInfo['uid'], $shop_id, $cart_num);
                if($getId){
                    $temp = [
                        'uid' => $roleInfo['uid'],
                        'ypjg' => round($roleInfo['ypjg'] - $money),
                    ];
                    model('Role')->saved($temp);

                    returnJson(1, '购买成功！[花费：'.$money.']');
                }else{
                    returnJson(0, '系统繁忙');
                }
            } else {
                returnJson(0, '非法请求');
            }
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

}
